package websocket.app.scheduler;

import java.util.Random;

import javax.annotation.PostConstruct;
import javax.ejb.Schedule;
import javax.ejb.Singleton;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import websocket.app.messaging.ListenersRegistry;
import websocket.app.messaging.MemoryStatusMessage;
import websocket.app.messaging.Message;
import websocket.app.messaging.MessageListener;


@Singleton
public class Timer {
    private Random random = new Random();
    
    private final static Log log = LogFactory.getLog(Timer.class);

    @PostConstruct
    public void postConstruct() {
	log.info("Timer created");
    }

    @Schedule(second = "*/1", minute = "*", hour = "*")
    public void sendMessage() {
	Message message = new MemoryStatusMessage(random.nextInt(1024));
	
	for (MessageListener listener : ListenersRegistry.getInstance().getAllListeners()) {
	    listener.onMessage(message);
	}
    }

}
